package com.pengzy.comment.web.result;

import com.pengzy.comment.web.redis.RedisCache;
import lombok.Data;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import reactor.util.context.Context;
import reactor.util.context.ContextView;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @author xiaozhi 2023/7/19
 */

@Component
public class ReactiveRequestContextHolder {

   // public static final Class<ServerHttpRequest> CONTEXT_KEY = ServerHttpRequest.class;



    public Mono<ServerHttpRequest> getRequest() {
        return Mono.deferContextual(ctx -> Mono.just(ctx.get(Info.CONTEXT_KEY).getRequest()));
//        return Mono.subscriberContext()
//                .map(ctx -> ctx.get(Info.CONTEXT_KEY).getRequest());
    }


    public  Mono<ServerHttpRequest> getRequest2() {
        return Mono.subscriberContext()
                .map(ctx -> {
                    System.out.println(ctx.get(Info.CONTEXT_KEY).getRequest());
                    return ctx.get(Info.CONTEXT_KEY).getRequest();
                });
    }

    public  ServerHttpRequest getRequest3() {
        return Infos.serverWebExchange.getRequest();
    }



    //获取当前response
    public Mono<ServerHttpResponse> getResponse(){
        return Mono.subscriberContext()
                .map(context -> context.get(Info.CONTEXT_KEY).getResponse());
    }

    public static final class Info{
        public static final Class<ServerWebExchange> CONTEXT_KEY = ServerWebExchange.class;
        public static final String CONTEXT_KEY_TO = "exchange";
    }

    @Data
    public static class Infos{
        public static ServerWebExchange serverWebExchange;
    }


}
